﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTracker.Models;
using ScrumTracker.Helpers;
using System.Web.Helpers;

namespace ScrumTracker.Controllers
{
    public class CommentsController : Controller
    {
        //
        // GET: /Comments/

        public ActionResult Create(Comment model)
        {
            model.CreatedAt = DateTime.UtcNow;
            model.UserId = SessionHelper.CurrentUser.UserId;
            if (ModelState.IsValid)
            {
                var ctx = ScrumTrackerEntities.Instance;
                ctx.Comments.AddObject(model);
                ctx.SaveChanges();
                var grid = new WebGrid(ctx.Comments.Where(c => c.IssueId == model.IssueId).OrderByDescending(s => s.CreatedAt), canSort: false, canPage: false);
                return Json(new { success = true, html = grid.GetHtml(tableStyle: "table-issues", headerStyle: "ui-widget-header").ToHtmlString() });
            }
            return Json(new { success = false });
        }

    }
}
